How to Point sprite's direction towards Mouse or an Object [duplicate]

Posted by Irfan Dahir on Game Development See other posts from Game Development or by Irfan Dahir
Published on 2013-11-03T19:26:08Z Indexed on 2013/11/03 22:20 UTC
Read the original article Hit count: 224

Filed under:
|
|
|
|

This question already has an answer here:

I need some help with rotating sprites towards the mouse. I'm currently using the library allegro 5.XX. The rotation of the sprite works but it's constantly inaccurate. It's always a few angles off from the mouse to the left. Can anyone please help me with this? Thank you. P.S I got help with the rotating function from here: http://www.gamefromscratch.com/post/2012/11/18/GameDev-math-recipes-Rotating-to-face-a-point.aspx Although it's by javascript, the maths function is the same. And also, by placing:

if(angle < 0)
{
angle = 360 - (-angle);
}

doesn't fix it.

The Code:

#include <allegro5\allegro.h>
#include <allegro5\allegro_image.h>
#include "math.h"

int main(void)
{

    int width = 640;
    int height = 480;
    bool exit = false;

    int shipW = 0;
    int shipH = 0;




    ALLEGRO_DISPLAY *display = NULL;
    ALLEGRO_EVENT_QUEUE *event_queue = NULL;
    ALLEGRO_BITMAP *ship = NULL;


    if(!al_init()) return -1;

    display = al_create_display(width, height);

    if(!display)
    return -1;

    al_install_keyboard();
    al_install_mouse();
    al_init_image_addon();
    al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); //smoother rotate

    ship = al_load_bitmap("ship.bmp"); 
    shipH = al_get_bitmap_height(ship);
    shipW = al_get_bitmap_width(ship);

    int shipx = width/2 - shipW/2;
    int shipy = height/2 - shipH/2;
    int mx = width/2;
    int my = height/2;

    al_set_mouse_xy(display, mx, my);
    event_queue = al_create_event_queue();

    al_register_event_source(event_queue, al_get_mouse_event_source());
    al_register_event_source(event_queue, al_get_keyboard_event_source());
    //al_hide_mouse_cursor(display);
    float angle;

    while(!exit)
    {
    ALLEGRO_EVENT ev;
    al_wait_for_event(event_queue, &ev);

    if(ev.type == ALLEGRO_EVENT_KEY_UP)
    {
               switch(ev.keyboard.keycode)
               {
               case ALLEGRO_KEY_ESCAPE:
               exit = true;
               break;


               /*case ALLEGRO_KEY_LEFT:
                    degree -= 10;

               break;

               case ALLEGRO_KEY_RIGHT:
                    degree += 10;

               break;*/


               case ALLEGRO_KEY_W:
               shipy -=10;
               break;
               case ALLEGRO_KEY_S:
               shipy +=10;
               break;
               case ALLEGRO_KEY_A:
               shipx -=10;
               break;
               case ALLEGRO_KEY_D:
               shipx += 10;
               break;




               }
    }else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES)
    {
          mx = ev.mouse.x;
          my = ev.mouse.y;
angle = atan2(my - shipy, mx - shipx);



}


   // al_draw_bitmap(ship,shipx, shipy, 0);
    //al_draw_rotated_bitmap(ship, shipW/2, shipH/2, shipx, shipy, degree * 3.142/180,0);
    al_draw_rotated_bitmap(ship, shipW/2, shipH/2, shipx, shipy,angle, 0);
    //I directly placed the angle because the allegro library calculates radians, and if i multiplied it by 180/3.    142 the rotation would go hawire, not would, it actually did.
    al_flip_display();
    al_clear_to_color(al_map_rgb(0,0,0));
}

al_destroy_bitmap(ship);
al_destroy_event_queue(event_queue);
al_destroy_display(display);
return 0;
}

EDIT: This was marked duplicate by a moderator. I'd like to say that this isn't the same as that. I'm a total beginner at game programming, I had a view at that other topic and I had difficulty understanding it. Please understand this, thank you. :/

Also, while I was making a print of what the angle is I got this... Here is a screenshot:http://img34.imageshack.us/img34/7396/fzuq.jpg Which is weird because aren't angles supposed to be 360 degrees only?

© Game Development or respective owner

Related posts about c++

Related posts about rotation